package dominio;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

@Entity
@Table(name = "solicitudCompra", uniqueConstraints = { @UniqueConstraint(columnNames = { "idSolicitud" }) })
public class SolicitudCompra implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -811970328117941741L;

	@Id
	@GeneratedValue
	private Long codigo;

	// es el id de la solicitud del deposito
	private Long idSolicitud;

	@OneToMany(cascade = CascadeType.ALL, mappedBy = "solicitudCompra", orphanRemoval = true, fetch = FetchType.EAGER)
	private List<ArticuloItemCompra> articulosItemsCompra;

	private Estado estado;

	public SolicitudCompra() {
		estado = Estado.CREADA;
	}

	public SolicitudCompra(Long idSolicitud,
			List<ArticuloItemCompra> articulosItems) {
		this();
		this.idSolicitud = idSolicitud;
		this.articulosItemsCompra = articulosItems;
	}

	public SolicitudCompra(Long idSolicitud) {
		this();
		this.idSolicitud = idSolicitud;
	}

	public Long getCodigo() {
		return codigo;
	}

	public void setCodigo(Long codigo) {
		this.codigo = codigo;
	}

	public List<ArticuloItemCompra> getArticulosItemsCompra() {
		return articulosItemsCompra;
	}

	public void setArticulosItemsCompra(List<ArticuloItemCompra> articulosItems) {
		this.articulosItemsCompra = articulosItems;
	}

	public Long getIdSolicitud() {
		return idSolicitud;
	}

	public void setIdSolicitud(Long idSolicitud) {
		this.idSolicitud = idSolicitud;
	}

	public Estado getEstado() {
		return estado;
	}

	public void agregar() {
		for (ArticuloItemCompra articuloItem : this.articulosItemsCompra) {
			articuloItem.setSolicitudCompra(this);
		}
	}

	public void entregar() {
		for (ArticuloItemCompra articuloItem : articulosItemsCompra) {
			if (!articuloItem.entregado()) {
				return;
			}
		}
		this.estado = Estado.ENTREGADA;
	}

	public boolean estaEntregada() {
		return Estado.ENTREGADA.equals(estado);
	}

	public String toXML() throws ParserConfigurationException,
			TransformerException {
		String xmlFile = null;
		DocumentBuilderFactory docFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

		// root elements
		Document doc = docBuilder.newDocument();
		Element rootElement = doc.createElement("solicitudFabrica");
		doc.appendChild(rootElement);

		// codigo elements
		Element idSolicitud = doc.createElement("idSolicitud");
		idSolicitud
				.appendChild(doc.createTextNode(getIdSolicitud().toString()));
		rootElement.appendChild(idSolicitud);

		// codigo elements
		Element estado = doc.createElement("estado");
		estado.appendChild(doc.createTextNode(Estado.ENTREGADA.toString()));
		rootElement.appendChild(estado);

		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		StreamResult result = new StreamResult(baos);

		transformer.transform(source, result);
		System.out.println("File saved!");
		OutputStream outputStream = result.getOutputStream();
		xmlFile = outputStream.toString();

		return xmlFile;

	}
}
